﻿using System;
using System.ComponentModel;

namespace MyFramework.Helpers
{
    /// <summary>
    /// Enum helpers
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// Gets the enum from int.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <param name="enumeratorType">Type of the enumerator.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">Si e' verificato un errore nella creazione dell' Enumeratore Generico </exception>
        public static T GetEnumFromInt<T>(int value, out T enumeratorType)
        {
            enumeratorType = default(T);
            try
            {
                T result;
                result = (T)Enum.Parse(enumeratorType.GetType(), value.ToString());
                enumeratorType = result;
                return enumeratorType;

            }
            catch (Exception)
            {
                throw new Exception("Si e' verificato un errore nella creazione dell' Enumeratore Generico ");
            }
        }

        /// <summary>
        /// Gets the enum from int.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">Si e' verificato un errore nella creazione dell' Enumeratore Generico </exception>
        public static T GetEnumFromInt<T>(int value)
        {

            try
            {
                T result;
                result = (T)Enum.Parse(typeof(T), value.ToString());
                return result;

            }
            catch (Exception)
            {
                throw new Exception("Si e' verificato un errore nella creazione dell' Enumeratore Generico ");
            }
        }

        /// <summary>
        /// Gets the enum from string.
        /// </summary>
        /// <typeparam name="TEnum">The type of the enum.</typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">
        /// </exception>
        public static TEnum GetEnumFromString<TEnum>(string value)
        {
            TEnum result = default(TEnum);

            if (result is Enum)
            {
                if (Enum.IsDefined(typeof(TEnum), value))
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(TEnum));
                    result = (TEnum)converter.ConvertFromString(value);
                }
                else
                {
                    throw new ArgumentException(string.Format("Il valore: {0} non è definito in questo enum!!!!"), value);
                }
            }
            else
            {
                throw new ArgumentException(string.Format("Il tipo: {0} non è un enumeratore!!!!"), typeof(TEnum).ToString());
            }

            return result;
        }
    }
}
